package com.sport.ui.slideshow;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.sport.R;
import com.sport.databinding.FragmentSlideshowBinding;
import com.sport.pojo.AllCourses;
import com.sport.util.MySQLitOpenHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class SlideshowFragment extends Fragment {
    private FragmentSlideshowBinding binding;
    private ArrayList<AllCourses> coursesList = new ArrayList<AllCourses>(); //persons
    GridView gv;
    Button bt;
    boolean isLike = false;
    MySQLitOpenHelper db;
    MyAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentSlideshowBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        gv = view.findViewById(R.id.gv_list);
        db = new MySQLitOpenHelper(this.getContext());
//        读取课程数据
        db = new MySQLitOpenHelper(binding.getRoot().getContext());
        List<AllCourses> courses = db.showAllCourses();
        coursesList.addAll(courses);
        adapter = new MyAdapter();
        gv.setAdapter(adapter);
        return view;
    }


    public static int getimages(String name) {
        Class drawable = R.drawable.class;
        Field field = null;
        try {
            field = drawable.getField(name);
            int images = field.getInt(field.getName());
            return images;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return coursesList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View itemview;
            if (view == null) {
                itemview = View.inflate(SlideshowFragment.this.getContext(), R.layout.recycler_item_like, null);
            } else {
                itemview = view;
            }
            TextView tv_name = itemview.findViewById(R.id.tv_title);
            ImageView iv_img = itemview.findViewById(R.id.iv_img);
            ImageView iv_isLike = itemview.findViewById(R.id.iv_isLike);
            iv_isLike.setOnClickListener(new View.OnClickListener() {
                //为找到的button设置监听
                @Override
                //重写onClick函数
                public void onClick(View view) {
                    isLike = !isLike;//点击的时候改变该值
                    if (isLike) {
                        iv_isLike.setImageResource(R.drawable.liked);
                    } else {
                        iv_isLike.setImageResource(R.drawable.like);
                    }
                }
            });

            AllCourses course = coursesList.get(i);
            tv_name.setText(course.title);
            iv_img.setImageResource(getimages(course.img));
            return itemview;
        }
    }
}
